package day13;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.junit.Test;

public class JDBCDemo {

	// library
	@Test
	public void test1() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			//1、加载驱动Class.forName("");
			Class.forName("com.mysql.jdbc.Driver");
			//2、获得连接对象Connection
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java202007?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			//3、写sql语句
			String sql = "SELECT `id`,`name`,`age`,`gender` FROM `student`";
			//4、创建Statement(一艘船)
			statement = connection.createStatement();
			//5、执行sql语句
			//     (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
			//           返回值：int，表示你影响的行数
			//      (2)查询（没有改变表里面数据）:  select                              executeQuery()
			//           返回值：结果集ResultSet
			resultSet = statement.executeQuery(sql);
			ArrayList<Student> list = new ArrayList<Student>();
			// Moves the cursor forward one row from its current position
			// Returns:true if the new current row is valid; false if there are no more rows
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				// 每遍历一行就是查询出一个学生的信息，就可以创建一个学生对象
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//6、关闭连接
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	// Statement 有sql注入的问题
	@Test
	public void test2() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		// String usersName = "lisi11";
		String usersName = "zhangsan' OR 1=1 -- y";
		String usersPassword = "2323123";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java202007?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			// SELECT * FROM users WHERE `name`='zhangsan' OR 1=1 -- y' AND `password`='343';
			String sql = "SELECT * FROM users WHERE `name`='"+usersName+"' AND `password`='"+usersPassword+"'";
			statement = connection.createStatement();
			System.out.println(statement);
			resultSet = statement.executeQuery(sql);
			ArrayList<User> list = new ArrayList<User>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				String password = resultSet.getString("password");
				User user = new User(id, name, password);
				list.add(user);
			}
			for (User user : list) {
				System.out.println(user);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	// PreparedStatement
	@Test
	public void test3() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java202007?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			String sql = "SELECT `id`,`name`,`age`,`gender` FROM `student`";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//6、关闭连接
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void test4() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`name`,`age`,`gender` FROM `student`";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			System.out.println(statement);
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
	}


	@Test
	public void testSelectLike() {
		String studentName = "三";
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`name`,`age`,`gender` FROM `student` where name like ?";
			statement = connection.prepareStatement(sql);
			// statement.setString(1, "%张%");
			statement.setString(1, "%" + studentName + "%");
			resultSet = statement.executeQuery();
			System.out.println(statement);
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
	}
	
	@Test
	public void testInsert() {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO student(`name`,age,gender) VALUES(?,?,?)";
			statement = connection.prepareStatement(sql);
			// 填上?占位符
			statement.setString(1, "张三");
			statement.setInt(2, 23);
			statement.setString(3, "男");
			int count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	
	@Test
	public void testDelete() {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, 13);
			int count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	@Test
	public void testUpdate() {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set name=?,age=?,gender=?  where id=?";
			statement = connection.prepareStatement(sql);
			statement.setString(1, "李四");
			statement.setInt(2, 25);
			statement.setString(3, "女");
			statement.setInt(4, 11);
			int count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	
	
}
